package org.anti996.lms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.anti996.lms.model.Booking;
import org.anti996.lms.model.Driver;
import org.anti996.lms.model.Transport;
import org.anti996.lms.model.Truck;
import org.anti996.lms.service.BookingService;
import org.anti996.lms.service.DriverService;
import org.anti996.lms.service.TransportService;
import org.anti996.lms.service.TruckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 司机表 前端控制器
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */

@Slf4j
@RestController
@RequestMapping("/lms/driver")
@CrossOrigin(origins = "*")
public class DriverController {
    @Autowired
    private DriverService driverService;
    @Autowired
    private TransportService transportService;
    @Autowired
    private TruckService truckService;
    @Autowired
    private BookingService bookingService;

    /**
     * 获取司机列表
     *
     * @return 司机员列表
     */
    @GetMapping
    public List<Driver> driversList() { return driverService.list(); }


    /**
     * 获取司机分页列表
     *
     * @param no 第no个页面
     * @param sz 每个页面的数据条数
     * @return 分页司机员列表
     */
    @GetMapping("/page/{sz}/{no}")
    public List<Driver> driversListPage(@PathVariable(name = "no") int no,
                                            @PathVariable(name = "sz") int sz) {
        Page<Driver> page = driverService.page(new Page<>(no, sz));
        return page.getRecords();
    }

    /**
     * 根据司机编号获取司机信息
     *
     * @param d_id 司机编号
     * @return 查询到的司机信息
     */
    @GetMapping("/{id}")
    public Driver operator(@PathVariable(name = "id") int d_id) {
        return driverService.getById(d_id);
    }
    /**
     * 根据司机用户名获取司机id
     *
     * @param username 司机用户名
     * @return 查询到的司机id
     */
    @GetMapping("/getIdbyUsername/{username}")
    public int getIdbyUsername(@PathVariable(name = "username") String username) {
        Driver driver=driverService.getIdbyUsername(username);
        if (driver!=null)
            return driver.getDId();
        else return 0;}
    /**
     * 登陆
     */

    @PostMapping("/login")
    public  boolean login(@RequestParam String username,@RequestParam String password){
     return driverService.login(username,password);}


    /**
     * 新增司机
     *
     * @param driver 新增司机信息
     * @return 创建结果
     */
    @PostMapping(consumes = "application/json")
    public boolean insertDriver(@RequestBody Driver driver) {
        return driverService.save(driver);
    }

    /**
     * 更新司机信息
     *
     * @param d_id      司机编号
     * @param driver 新的司机信息
     * @return 更新是否成功
     */
    @PutMapping(path = "/{id}", consumes = "application/json")
    public boolean updateDriver(@PathVariable(name = "id") int d_id,
                                  @RequestBody Driver driver) {
        return driverService.updateById(driver);
    }

    /**
     * 删除指定司机的数据
     *
     * @param d_id 司机编号
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean removeDriver(@PathVariable(name = "id") int d_id) {
        return driverService.removeById(d_id);
    }

    //司机确认出发
    @PostMapping("/begin/{id}")
    public boolean begin(@PathVariable(name = "id") int d_id){
        Transport transport=transportService.findunfinish(d_id);//得到司机已分配任务运单
        LocalDateTime localDateTime=LocalDateTime.now();
        transport.setDepart(localDateTime);
        return transportService.updateById(transport);
    }


    //司机确认到达
    @PostMapping("/arrive/{id}")
    public boolean arrive(@PathVariable(name = "id") int d_id){
        Transport transport=transportService.findunarrive(d_id);//得到司机正在运输的运单
        LocalDateTime localDateTime = LocalDateTime.now();
        transport.setArrive(localDateTime);
        boolean f = transportService.updateById(transport);
        Driver driver = driverService.getById(d_id);
        driver.setWId(transport.getWIdTo());
        driver.setState(0);
        boolean g = driverService.updateById(driver);
        List<Booking> list =bookingService.findBytNum(transport.getTNum());
        for (int i=0;i<list.size();i++){
            Booking booking = list.get(i);
            booking.setArrive(localDateTime);
            boolean p = bookingService.updateById(booking);
        }
        Truck truck = truckService.getById(transport.getTId());
        truck.setWId(transport.getWIdTo());
        truck.setState(0);
        return truckService.updateById(truck);
    }
}
